using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;

namespace Elementum
{
    class RayCast : Constant
    {
        protected World _world;
        protected RayShot _rayShot;
        List<RayShot> _rayList;
        private int _count = 30;
        //private RayShot _rayShot;
        //private float _range;


        public RayCast(World world)
        {
            this._world = world;
            _rayList = new List<RayShot>();

            for (int i = 0; i < _count; i++) {
                RayShot shot = new RayShot();
                shot.Length = 0;
                _rayList.Add(shot);
            }
        }

        public List<RayShot> RayList 
        {
            get 
            {
                return _rayList;
            }
        }

        public Vector2 Position { get; set; }

        public float Range { get; set;}

        public virtual void UpDate()
        {
            scanRay();
        }

        public float BaseAngle { get; set; }

        public float ScanRange { get; set; }

        private void scanRay()
        {
            Vector2 position = Position/PIXEL_TO_METER;
            for (int i = 0; i < _count; i++)
            {
                double angle = (i * ScanRange) / (double)_count + BaseAngle - ScanRange/2;
                double r = ((double)angle) * PI / 180;
                Vector2 point2 = new Vector2(position.X + (float)(Range * Math.Cos(r) / (float)PIXEL_TO_METER),
                        position.Y + (float)(Range * Math.Sin(r) /(float) PIXEL_TO_METER));
                _rayShot = _rayList[i];
                _rayShot.Angle = (float)r;
                _rayShot.IsHit = false;
                _rayShot.ContactPoint = Vector2.Zero;
                _rayShot.Length = Range;
                _rayShot.StartPoint = Position;
                _rayShot.EndPoint = point2;
                _world.RayCast(RayCastFunc, position, point2);
            }
            //addRealm();
            //creatRealm();
        }

        public float RayCastFunc(Fixture fixture, Vector2 point, Vector2 normal, float fraction)
        {
            Body body = fixture.GetBody();

            //if (body.GetUserData() == "player")
            //    return -1;

            //if (body.GetUserData() == "ground")
            {
                _rayShot.IsHit = true;
                float d = Range * fraction ;
                float rad = Range / PIXEL_TO_METER;
                double angle = _rayShot.Angle;
                _rayShot.ContactPoint = point;
                _rayShot.Length = d;
                //Vector2 start = new Vector2(centerBody.getPosition().x + d * (float)Math.cos(angle),//inter point
                //        centerBody.getPosition().y + d * (float)Math.sin(angle));

                //Vector2 end = new Vector2(centerBody.getPosition().x + rad * (float)Math.cos(angle),//radius point
                //        centerBody.getPosition().y + rad * (float)Math.sin(angle)); 



            }

            return fraction;
        }

    }
}
